<template>
  <q-page class="cc-admin">
    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table flat color="primary" class="cross_table" separator="cell" :columns="columns" :data="list" row-key="id" :pagination.sync="pagination" :visible-columns="group" @request="query" :rows-per-page-options="[10, 20, 50, 100]" selection="multiple" :selected.sync="selected">
        <template v-slot:top="">
          <div class="row no-wrap full-width">
            <q-input clearable outlined dense placeholder="请输入关键字搜索" class="on-left" @input="query" debounce="500" v-model="key">
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />
            <div class="q-gutter-xs" style="margin-left: 10px">
              <q-btn icon="mdi-file-search-outline" no-wrap color="primary" label="查看错题分布" @click="viewEcharts" />
            </div>
          </div>
        </template>
        <template #body-cell-question="props">
          <q-td :props="props" :auto-width="true">
            <div style="max-width: 800px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ props.row.question }}</div>
          </q-td>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="mdi-file-search-outline" @click="edit(props.row)"> <q-tooltip>编辑</q-tooltip></q-btn>
            <btn-del label="选择题" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="submit" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          {{ editType }}选择题
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-12">
              <h5>一、选择题</h5>
              <!-- <q-input outlined dense v-model="" type="text" /> -->
            </div>
            <div style="display: flex; flex-direction: row">
              <div class="col-12">{{ form.question }}</div>
            </div>
            <div class="col-12">
              <h5>实际答案：{{ form.realAnswer }}</h5>
            </div>
            <div class="col-12"  style="display: flex">
              <viewer class="label-list q-mb-sm" style="max-height: 150px" ref="viewer" :options="options" :image="form.viedoUrl" v-if="form.viedoUrl" @inited="inited">
                <img class="q-pl-sm" :src="form.viedoUrl" style="object-fit: contain; max-height: 100%; max-height: 150px" />
              </viewer>
            </div>
            <div class="col-12">
              <q-radio disable v-model="form.answer" color="cyan" val="A" @input="changeCheck({ value: 'A', index })">
                <template v-slot:default>
                  <div v-html="renderMath('A: ' + form.optionA)"></div>
                </template>
              </q-radio>
              <q-radio disable v-model="form.answer" color="cyan" val="B" @input="changeCheck({ value: 'B', index })">
                <template v-slot:default>
                  <div v-html="renderMath('B: ' + form.optionB)"></div>
                </template>
              </q-radio>
              <q-radio disable v-model="form.answer" color="cyan" val="C" @input="changeCheck({ value: 'C', index })">
                <template v-slot:default>
                  <div v-html="renderMath('C: ' + form.optionC)"></div>
                </template>
              </q-radio>
              <q-radio disable v-model="form.answer" color="cyan" val="D" @input="changeCheck({ value: 'D', index })">
                <template v-slot:default>
                  <div v-html="renderMath('D: ' + form.optionD)"></div>
                </template>
              </q-radio>
            </div>
            <!-- 答案解析条件需要改为作答完毕 -->
            <h5 class="text-red-6 blankAnalysis" v-html="renderMath(form.analysis)"></h5>
            <div class="col-12" style="display: flex; align-items: center">
              <div class="q-video" style="margin-top: 10px" v-if="imageExtensions.includes(form.viedoUrl2 && form.viedoUrl2.substring(form.viedoUrl2.lastIndexOf('.')))">
                <viewer class="label-list q-mb-sm" style="max-height: 100px" ref="viewer" :options="options" :image="form.viedoUrl2" v-if="form.viedoUrl2" @inited="inited">
                  <img class="q-pl-sm" :src="form.viedoUrl2" style="object-fit: contain; max-height: 100%; max-width: 150px" />
                </viewer>
              </div>
              <div class="q-video" style="margin-top: 10px" v-else>
                <iframe :src="form.viedoUrl2" frameborder="0" allowfullscreen height="100%" :ratio="4 / 3" />
              </div>
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <!-- <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit" /> -->
        </div>
      </q-form>
    </q-dialog>
    <q-dialog maximized flat persistent ref="EchartsDialog">
      <q-form class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          错题分布
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <!-- <div class="row q-col-gutter-x-md dialog_form q-pa-md">
                        <div id="echartsBar" style="width: 100%; height: 60vh"></div>
                    </div> -->
          <echartsBar ref="echartsBarview"></echartsBar>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <q-inner-loading :showing="loading">
      <q-spinner-gears size="50px" color="primary" />
    </q-inner-loading>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import 'katex/dist/katex.min.css';
import katex from 'katex';
import * as echarts from 'echarts';
import echartsBar from 'src/pages/postgraduate/echarts/echarts-bar.vue';
import Viewer from 'viewerjs';

export default {
  name: 'ChoiceErrorQuestions',
  mixins: [IndexMixin],
  components: {
    confirm,
    echartsBar,
  },
  data() {
    return {
      catalog: '01',
      form: {
        subjectId: '',
        subjectText: '',
        majorId: '',
        majorText: '',
        categorys: [],
        categoryIds: '',
        categoryIdsText: '',
        question: '',
        optionA: '',
        optionB: '',
        optionC: '',
        optionD: '',
        answer: '',
        realAnswer: '',
        analysis: '',
        videoUrl: '',
      },
      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
        },
        {
          name: 'question',
          align: 'left',
          label: '题目',
          field: 'question',
        },
        {
          name: 'realAnswer',
          align: 'left',
          label: '实际答案',
          field: 'realAnswer',
        },
        {
          name: 'opt',
          align: 'center',
          label: '操作',
          field: 'id',
        },
      ],
      echartsList: [],
      xList: [],
      yList: [],
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/choice/errorQuestions/list',
        getNoVideoQuestion: '/choice/errorQuestions/getNoVideoQuestion',
        delete: '/choice/errorQuestions/delete',
      },
      NoVideoQuestion: [],
    };
  },
  methods: {
    // 初始化预览组件
    inited(viewer) {
      this.$viewer = viewer;
    },
    renderMath(str) {
      return katex.renderToString(str, {
        throwOnError: false,
      });
    },

    viewEcharts() {
      this.$refs.EchartsDialog.show();
      this.$nextTick(() => {
        this.$refs.echartsBarview.show(this.list);
      });
    },
    getDictLabel,
    initDict() {
      this.$axios.get(this.url.getNoVideoQuestion).then((res) => {
        this.NoVideoQuestion = res;
        console.log(this.NoVideoQuestion);
      });
    },
  },
  mounted() {
    this.initDict();
  },
};
</script>

<style lang="stylus"></style>
